package com.store.app.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hyphenate.easeui.widget.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.store.app.BaseActivity;
import com.store.app.R;
import com.store.app.bean.HelperBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;

import org.json.JSONObject;

/**
 *店東助手頁面
 */
public class ContactHelperActivity extends BaseActivity implements IOnHttpListener,View.OnClickListener {

    private ImageView public_ll_return;
    private TextView tvTitle;
    private TextView submit;
    private CircleImageView iv_head;
    private TextView tv_name;
    private TextView tv_phone;
    private TextView tv_calls;
    private TextView tv_desc;
    private NetRequest netRequest;
    DisplayImageOptions options;
    private String assistant_id;//获取评价信息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacn_helper);
        initView();
        netRequest = new NetRequest(this);
        netRequest.requestGetHelper(1);
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .showImageOnLoading(R.drawable.pic_icon_customer)
                .showImageOnFail(R.drawable.pic_icon_customer)
                .showStubImage(R.drawable.pic_icon_customer)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }

    @Override
    public void onAfinalFail() {

    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if(requestId == 1){
            Gson gson = new Gson();
            try{
                JSONObject jsonObject = new JSONObject(result);
                HelperBean bean = gson.fromJson(jsonObject.toString(), HelperBean.class);
                String headPic = bean.getHead_pic_path();
                String name = bean.getName();
                String tel = bean.getMobile();
                String intro = bean.getRemark();
                assistant_id=bean.getAssistant_id();
                tv_name.setText(name);
                tv_phone.setText(tel);
                tv_desc.setText(intro);
                if(!TextUtils.isEmpty(headPic)){
                    ImageLoader.getInstance().displayImage(headPic, iv_head, options);
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        showToast(msg);
    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        public_ll_return.setOnClickListener(this);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        submit = (TextView) findViewById(R.id.submit);
        submit.setOnClickListener(this);
        iv_head = (CircleImageView) findViewById(R.id.iv_head);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_calls = (TextView) findViewById(R.id.tv_calls);
        tv_calls.setOnClickListener(this);
        tv_desc = (TextView) findViewById(R.id.tv_desc);
        tvTitle.setText("我的客服");
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.submit:
                if(TextUtils.isEmpty(assistant_id)){
                    Toast.makeText(this,"还未绑定我的客服",Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent2 = new Intent(ContactHelperActivity.this, EvaluateServiceActivity.class);
                    intent2.putExtra("assistant_id",assistant_id);
                    startActivity(intent2);
                }
                break;
            case R.id.tv_calls:
                String phoneNumber = tv_phone.getText().toString().trim();
                if(!TextUtils.isEmpty(phoneNumber)){
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
                    startActivity(intent);
                }
                break;
            case R.id.public_ll_return:
                finish();
                break;
        }
    }
}
